<template>
  <div class="wrap">
    <p class="p1">商品详情</p>
    <div class="body">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="商品名称">
          <el-input v-model="form.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品描述">
          <el-input v-model="form.subtitle" disabled></el-input>
        </el-form-item>
        <el-form-item label="当前状态">
          <el-input v-model="form.status" disabled></el-input>
        </el-form-item>
        <el-form-item label="所属分类">
          <el-select v-model="options" value-key="id" @change="change">
            <el-option
              v-for="(item, index) in option"
              :key="index"
              :label="item.name"
              :value="item"
              disabled
            ></el-option>
          </el-select>
          <el-select v-model="options2" v-if="optionFlag != true">
            <el-option
              v-for="(item, index) in option2"
              :key="index"
              :value="item.name"
              disabled
              >{{ item.name }}</el-option
            >
          </el-select>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="form.price" class="price" disabled></el-input>
          <span>元</span>
        </el-form-item>
        <el-form-item label="商品价格">
          <el-input v-model="form.stock" class="price" disabled></el-input>
          <span>件</span>
        </el-form-item>
        <el-form-item label="商品图片">
          <div class="img" >
              <img v-for="(item,index) in src" :key="index" :src="item" alt="" disabled />
          </div>
        </el-form-item>
        <el-form-item label="商品详情">
          <el-input v-html="form.detail" class="price"></el-input>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getdetail, getcategory } from "@/utils/request.js";
export default {
  name: "Detail",
  data() {
    return {
      form: {},
      productId: this.$route.query.productId,
      categoryId: "",
      option: [],
      option2: [],
      options: "",
      options2: "",
      optionFlag: true,
      src: [],
    };
  },
  created() {
    this.getlist();
  },
  mounted() {},
  methods: {
    onSubmit() {
      // console.log("submit!");
    },
    getlist() {
      getdetail(this.productId).then((res) => {
        this.form = res.data;
        // 将图片地址字符串截取转为数组 循环渲染到页面
        let arr= res.data.subImages.split(',')
        arr.forEach((item) => {
          this.src.push(`${res.data.imageHost}${item}`)
        })
        // console.log(this.src);
        this.categoryId = res.data.categoryId;
        this.getgory();
      });
      getcategory(0).then((res) => {
        // console.log(res);
        this.option = res.data;
        this.options = this.option[this.productId].name;
      });
    },
    // 二级菜单
    getgory() {
      getcategory(this.categoryId).then((res) => {
        // console.log(res);
        if (res.data == "") {
          this.optionFlag = true;
        } else {
          this.optionFlag = false;
          this.option2 = res.data;
          this.options2 = this.option2[0].name;
        }
      });
    },
    // 监听下拉菜单值的变化
    change(e) {
      // console.log(e);
      this.categoryId = e.id;
      this.getgory();
    },
  },
  components: {},
};
</script>

<style lang="scss" scoped>
.wrap {
  // height: calc(100vh - 60px);
  .p1 {
    text-align: left;
    font-size: 40px;
    margin: 10px 0 20px 0;
  }
  .body {
    width: 600px;
    margin: 120px auto;
    text-align: left;
    .price {
      width: 300px;
    }
    span {
      display: inline-block;
      width: 40px;
      height: 40px;
      border: 1px solid #ccc;
      border-top-right-radius: 5px;
      border-bottom-right-radius: 5px;
      text-align: center;
      margin-left: -1px;
    }
    .img {
      width: 100%;
      height: 200px;
      img{
        width: 100px;
        height: 100px;
      }
    }
  }
}
</style>
